package com.dzsw.redission.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.connection.balancer.RoundRobinLoadBalancer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class RedissonConfig {

    @Bean
    public RedissonClient redissonClient(SentinelServersConfig sentinelServersConfig) {
        Config config = new Config();
        config.useSentinelServers()
                .addSentinelAddress((String[])sentinelServersConfig.getSentinelAddresses().toArray())
                .setMasterName(sentinelServersConfig.getMasterName())
                .setReadMode(sentinelServersConfig.getReadMode())
                .setLoadBalancer(sentinelServersConfig.getLoadBalancer())
                .setTimeout(sentinelServersConfig.getTimeout());
        config.setNettyThreads(64);
        config.setCodec(new JsonJacksonCodec());
        return Redisson.create(config);
    }

    @Bean
    @ConditionalOnProperty(prefix = "redisson.sentinelServersConfig")
    SentinelServersConfig sentinelServersConfig() {
        return new SentinelServersConfig();
    }

}
